#set($layout = "blank.html")
<div class="space-4"></div>
<div class="row">
	<div class="col-xs-1"></div>
	<div class="col-xs-10">
		<form id="addUserForm">

			<div>
				<label> 组织关系 <small class="text-info">用户组织关系归属</small></label>
				<div class="row">
					<div class="col-xs-4">
						<select name="subsidiaryId" id="subsidiaryId" class="form-control">
							<option value="0">请选择分公司</option>
							<optgroup label="-------------------">
								<!-- 遍历省份 -->
								#foreach($subsidiary in $obj.data.subsidiaries)
								<!-- 遍历结束 -->
								<option value="$!subsidiary.id">$!subsidiary.name</option>
								#end
							</optgroup>
						</select>
					</div>
					<div class="col-xs-4">
						<select name="projectId" id="projectId" class="form-control">
							<option value="0">请选择项目部</option>
							<optgroup label="-------------------" id="projectSelector">
							</optgroup>
						</select>
					</div>
					<div class="col-xs-4">
						<select name="contractId" id="contractId" class="form-control">
							<option value="0">请选择合同</option>
							<optgroup label="-------------------" id="contractSelector">
							</optgroup>
						</select>
					</div>
				</div>

			</div>

			<div>
				<label> 登录名 <small class="text-info">系统登录账号</small></label>
				<div>
					<input class="form-control" type="text" placeholder="请输入登录名" name="name" data-type='required' data-error='请输入登录名' />
				</div>
			</div>

			<div>
				<label> 真实姓名 <small class="text-info">用户的真实姓名</small></label>
				<div>
					<input class="form-control" type="text" placeholder="请输入用户真实姓名" name="realName" data-type='required' data-error='请输入用户真实姓名'>
				</div>
			</div>
			<div>
				<label> 密码 <small class="text-info">系统登录密码</small></label>
				<div>
					<input class="form-control" type="password" placeholder="请输入密码" name="password" data-type='password' data-error='密码只能是字母和数字,长度不少于6位'>
				</div>
			</div>
			<div>
				<label> 确认密码 <small class="text-info">确认系统登录密码</small></label>
				<div>
					<input class="form-control" type="password" placeholder="请再次输入密码" name="rePassword" data-type='password' data-error='密码只能是字母和数字,长度不少于6位'>
				</div>
			</div>
			<div>
				<label> 电话 <small class="text-info">用户电话号码</small></label>
				<div>
					<input class="form-control" type="text" placeholder="请输入电话号码" name="phone">
				</div>
			</div>
			<div>
				<label> 邮箱 <small class="text-info">邮箱地址</small></label>
				<div>
					<input class="form-control" type="text" placeholder="请输入邮箱地址" name="email" >
				</div>
			</div>
			<div>
				<label> 用户状态 <small class="text-info">ON为可用 OFF为禁用</small></label>
				<div>
					<input type="hidden" name="status" value="ACTIVED"> <label> <input name="statusSwitcher" class="ace ace-switch ace-switch-4 btn-rotate" type="checkbox"
						checked="checked"> <span class="lbl"></span></label>
				</div>
			</div>
			<div class="pull-right" style="margin-right:45px">
				<button type="button" class="btn btn-sm btn-primary" onclick="save()">
					<i class="fa fa-save"></i> 提交
				</button>
				<button class="btn btn-sm btn-warning btn-dialog-undo">
					<i class="fa fa-undo"></i> 返回
				</button>
			</div>
		</form>
	</div>
	<div class="col-xs-1"></div>
</div>
<script>
	$(function() {
		//分公司切换
		$("#subsidiaryId").on('change', function() {
			if ($(this).val() > 0) {
				$.post(getRootPath() + "/project/listBySubsidiaryId", {
					id : $(this).val()
				}, function(result) {
					if (result.operationState == 'SUCCESS') {
						var projectSelector = $('#projectSelector').empty();
						$(result.data.projects).each(function(i, item) {
							projectSelector.append('<option value="'+item.id+'">' + item.name + '</option>')
						})
					} else {
						showMessage(result.data.reason);
					}
				}, 'json')
			} else {
				showMessage("请选择分公司");
			}
		})
		//项目切换
		$("#projectId").on('change', function() {
			if ($(this).val() > 0) {
				$.post(getRootPath() + "/contract/listByProjectId", {
					id : $(this).val()
				}, function(result) {
					if (result.operationState == 'SUCCESS') {
						var contractSelector = $('#contractSelector').empty();
						$(result.data.contracts).each(function(i, item) {
							contractSelector.append('<option value="'+item.id+'">' + item.name + '</option>')
						})
					} else {
						showMessage(result.data.reason);
					}
				}, 'json')
			} else {
				showMessage("请选择项目部");
			}
		})
	})
	function save() {
		if ($('input').validation()) {
			if ($('input[name=password]').val() == $('input[name=rePassword]').val()) {
				$('input[name=status]').val($('input[name=statusSwitcher]')[0].checked ? 'ACTIVED' : 'DISABLED');
				$.post(getRootPath() + '/user/add', $('#addUserForm').serialize(), commonAjaxCallBasck, 'json');
			} else {
				validationFail("两次输入的密码不一致", $('input[name=rePassword]')[0]);
			}
		}
	}
</script>